package cn.lwx.javaweb.web.action;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.lwx.javaweb.bean.*;
import cn.lwx.javaweb.utils.UserUtils;

/**
 * 注册
 * @author lwx
 * @date 2021-05-08
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		User user = new User();
		user.setUsername(username);
		user.setPassword(password);

		if(UserUtils.insert(user)==1) {
			System.out.println("注册成功");
			response.sendRedirect(request.getContextPath() + "/login.jsp");
		}else {
			System.out.println("注册失败");
			response.sendRedirect(request.getContextPath() + "/failregister.jsp");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
